<?php
declare (strict_types=1);

namespace app\common\model\customer;

use cores\BaseModel;
use think\model\relation\BelongsTo;

/**
 * 客户套餐模型
 * Class Package
 * @package app\common\model
 */
class Package extends BaseModel
{
    protected $name = 'customer_package';
    // 定义主键
    protected $pk = 'package_id';
    // 追加元素
    protected $append = [
        'package_service',
        'signature_time_text',
    ];

    public function getSignatureTimeTextAttr($value, $data)
    {
        return !empty($data['signature_time']) ? date("Y-m-d", $data['signature_time']) : '';
    }

    public function customer()
    {
        return $this->hasOne('Customer', 'customer_id', 'customer_id');
    }

    /**
     * 关联销售记录表
     * @return BelongsTo
     */
    public function storeUser(): BelongsTo
    {
        $module = self::getCalledModule();
        return $this->belongsTo("app\\{$module}\\model\\store\\User", 'store_user_id');
    }

    /**
     * 关联会计记录表
     * @return BelongsTo
     */
    public function kjUser(): BelongsTo
    {
        $module = self::getCalledModule();
        return $this->belongsTo("app\\{$module}\\model\\store\\User", 'kj_user_id');
    }

    /**
     * 关联外勤记录表
     * @return BelongsTo
     */
    public function wqUser(): BelongsTo
    {
        $module = self::getCalledModule();
        return $this->belongsTo("app\\{$module}\\model\\store\\User", 'wq_user_id');
    }


    public function getPackageServiceAttr($value, $data)
    {
        return (new Service())->where(['is_delete' => 0, 'package_id' => $data['package_id']])->select() ?: [];
    }
}
